package tr.gov.tubitak.bilgem.esya.certviewer.viwerasn;

import sun.security.x509.BasicConstraintsExtension;
import tr.gov.tubitak.bilgem.esya.certviewer.Bundle;

import java.io.IOException;
import java.util.Enumeration;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 06.12.2011
 * Time: 20:25
 * To change this template use File | Settings | File Templates.
 */
public class EBasicContraintsExtension implements ECertExtension {
    BasicConstraintsExtension basicConstraintsExtension;
    @Override
    public String getName() {
        return Bundle.getString("EBasicContraintsExtension.Name");
    }

    @Override
    public String getShortTitle()
    {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            Object is_ca = basicConstraintsExtension.get("is_ca");
            Object path_len = basicConstraintsExtension.get("path_len");
            Boolean  boolIsCa = (Boolean)is_ca;
            Integer intPathLen = (Integer)path_len;
            stringBuilder.append(Bundle.getString("EBasicContraintsExtension.isCA")+"=");

            String caStr="";
            if(boolIsCa)
            {
                caStr =  Bundle.getString("EBasicContraintsExtension.Exists");
            }
            else
            {
                caStr =  Bundle.getString("EBasicContraintsExtension.NotExists");
            }
            stringBuilder.append(caStr+"\n");

            stringBuilder.append(Bundle.getString("EBasicContraintsExtension.PathLenContrains")+"=");

            String pathLenStr="";
            if(intPathLen<0)
            {
                pathLenStr =  Bundle.getString("EBasicContraintsExtension.NotExists");
            }
            else
            {
                pathLenStr = ""+intPathLen;
            }
            stringBuilder.append(pathLenStr+"\n");
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return  stringBuilder.toString();
    }

    @Override
    public String getLongTitle() {
        return  getShortTitle();
    }

    @Override
    public boolean isCritical() {
        return basicConstraintsExtension.isCritical();
    }

    @Override
    public void encode(byte[] data) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setAsnObject(Object asnObject) {
        if(asnObject instanceof BasicConstraintsExtension)
        {
            basicConstraintsExtension = (BasicConstraintsExtension) asnObject;
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
